
function generateStatistics(fileUrl){
  //selectedMarksManager = new GMarkerManager({map: map});
  $.ajax({
    type: "GET",
    url: fileUrl,
    dataType: "xml",
    success: function(xml) {
      //Find all different stations
      var stations = [];
      var i=0;
      var barWidth= 0.5;
      
      $(xml).find('station').each(function(){
        var name = $(this).children('name').text();
        stations[i] = name;
        i++;
      });
      stations = stations.unique().sort();

      var stationsTicks = [stations.length];
      for(i=0; i<stations.length; i++){
        stationsTicks[i] = [i+(barWidth/2),stations[i]];
      }
      
      //alert(stationsTicks.toSource());

      //Find all different species
      i=0;
      var species = [];
      $(xml).find('species').each(function(){
        species[i] = $(this).find('genusSpecies').text();
        i++;
      });
      species = species.unique();
      //alert(species.toSource());
      
      i=0;
      var speciesStation2 = [];
      var speciesStation = [];
      $(xml).find('species').each(function(){
        speciesStation[i] = [stations.length];
        var k;
        for(k=0; k<stations.length; k++)speciesStation[i][k]=[k,0];
        $(this).children('record').each(function(){
          var stationName;
          $(this).children('station').each(function(){
            stationName = $(this).children('name').text();
          });
          var quantity = parseFloat($(this).find('quantity').text());
          var k = parseInt(stations.indexOf(stationName));
          speciesStation[i][k] = [k,quantity];
          speciesStation2.push(k,quantity);
        });
        i++;
      });
      //alert(speciesStation);
      var stack = 0, bars = true, lines = false, steps = false;

      function plotWithOptions() {
        return $.plot($("#chart"),  bindLabelAndData(speciesStation, species) , {
          series: {
            stack: stack,
            lines: {show: lines, steps: steps},
            bars: {show: bars, barWidth: barWidth}
          },
          xaxis: {
            ticks: stationsTicks
          }
        });
      }

      function bindLabelAndData(data, labels){
        var res = [];
        var i;
        for(i=0; i<speciesStation.length; i++){
          res.push({data: data[i], label: labels[i]});
        }
        return res;
      }

      plot = plotWithOptions();
    }//end of success
  });//end of ajax call
}

        

